app.controller("partRend", ['$scope', '$stateParams', '$http', '$window', '$cookieStore', '$state', '$timeout', '$filter', function ($scope, $stateParams, $http, $window, $cookieStore, $state, $timeout, $filter) {
    if ($stateParams.title == 'reserRend') {
        $window.document.title = '房间选定'
    } else {
        $window.document.title = '民宿详情'
    }
    // 判断登录函数
    $scope.login()
    $(function () {
        //日历插件
        $('#firstSelect').calendarSwitch({
            selectors: {
                sections: ".calendar"
            },
            index: 4,      //展示的月份个数
            animateFunction: "toggle",        //动画效果
            controlDay: false,//知否控制在daysnumber天之内，这个数值的设置前提是总显示天数大于90天
            daysnumber: "90",     //控制天数
            comeColor: "#66CCFF",       //入住颜色
            outColor: "#FF0033",      //离店颜色
            comeoutColor: "#FFCCCC",        //入住和离店之间的颜色
            callback: function () {
                $scope.startTime = $("#startDate").val();
                $scope.endTime = $("#endDate").val();
                $scope.dayNum = $("#dayNum").text()||1;
            },   //回调函数
            comfireBtn: '.comfire'//确定按钮的class或者id
        });
    })
    //获取民宿房间详情
    if ($stateParams.tit == '民宿') {
        $http({
            method: 'post',
            url: $scope.httptit + '/index/house/detail.html',
            data: {
                style: '2',
                id: $stateParams.id,
                userid: $cookieStore.get('login').id,
            },
            dataType: 'json',
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
            transformRequest: $scope.changeData
        }).then(function successCallback(response) {
            $scope.partDesc = response.data.data;
            console.log($scope.partDesc)
        }, function errorCallback(response) {
            console.log(response)
        });
    }
    //收藏与取消收藏
    $scope.shoucang = function (val, id) {
        $http({
            method: 'post',
            url: $scope.httptit + '/index/house/favorite.html',
            data: {
                status: val,
                houseid: id,
                userid: $cookieStore.get('login').id,
            },
            dataType: 'json',
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
            transformRequest: $scope.changeData
        }).then(function successCallback(response) {
            $scope.partDesc.collect = response.data.data
        }, function errorCallback(response) {
            console.log(response)
            // 请求失败执行代码
        });
    }

    $scope.reseHouse = function (id,status) {
        if(status==2){
            $.alert({
                title: '错误提示',
                text: '该房间已冻结',
            });
        }else if(status==3){
            $.alert({
                title: '错误提示',
                text: '该房间正在审核中',
            });
        }else if(status==5){
            $.alert({
                title: '错误提示',
                text: '该房间已被预订',
            });
        }else{
            $http({
                method: 'post',
                url: $scope.httptit + '/index/user/index.html',
                data: {
                    do: 'detail',
                    userid: $cookieStore.get('login').id,
                },
                dataType: 'json',
                headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
                transformRequest: $scope.changeData
            }).then(function successCallback(response) {
                console.log(response.data)
                if(response.data.data.cert_img_off&&response.data.data.cert_num){
                // if (1) {
    
                    $state.go('reserRend', { id: id, title: 'reserRend' })
                } else {
                    $.alert({
                        title: '错误提示',
                        text: '身份信息未完善，请完善身份信息',
                        onOK: function () {
                            //点击确认
                            $timeout(function () {
                                $state.go('userData', { 'id': 'userDesc' })
                            }, 60)
                        }
                    });
                }
    
            }, function errorCallback(response) {
                console.log(response)
                // 请求失败执行代码
            });
        }

    }
    // 订单房屋信息
    if ($stateParams.title == 'reserRend') {
        //切换租房方式
        $scope.radio = function (re) {
            $scope.level = re;
        }
        var date = new Date();
        // 点击立即支付
        $scope.handPay = function () {
            if ($scope.level) {
                if ($scope.level == 'day') {
                    if ($scope.startTime && $scope.endTime) {
                        // 生成订单
                        $http({
                            method: 'post',
                            url: $scope.httptit + '/index/order/build.html',
                            data: {
                                userid: $cookieStore.get('login').id,
                                houseid: $stateParams.id,
                                type: $scope.level,
                                startTime: Date.parse($scope.startTime) / 1000,
                                endTime: Date.parse($scope.endTime) / 1000,
                                dayNum: $scope.dayNum
                            },
                            dataType: 'json',
                            headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
                            transformRequest: $scope.changeData
                        }).then(function successCallback(response) {
                            if (response.data.status == 101) {
                                console.log(response.data)
                                $state.go('order', {title: 'order',  id:response.data.data })
                            } else if (response.data.status == 102) {
                                console.log(response.data)
                                $.alert({
                                    title: '错误提示',
                                    text: response.data.data,
                                    onOK: function () {
                                        //点击确认
                                        $timeout(function () {
                                            $state.go('userData', { 'id': 'userDesc' })
                                        }, 60)
                                    }
                                });
                            }else if(response.data.status==103){
                                console.log(response.data)
                                $.alert({
                                    title: '错误提示',
                                    text: response.data.data,    
                                });
                            }
                        }, function errorCallback(response) {
                            console.log(response)
                        });

                    } else {
                        $.alert({
                            title: '错误提示',
                            text: '未选择使用时间',
                        });
                    }
                } else if ($scope.level == 'noon') {
                    if (date)
                        // $cookieStore.put('orderDesc',
                        //     {
                        //         houseid: $stateParams.id,
                        //         type: $scope.level,
                        //         startTime: Date.parse(date),
                        //         endTime: Date.parse(date),
                        //         dayNum: 1
                        //     });
                    // 生成订单
                    $http({
                        method: 'post',
                        url: $scope.httptit + '/index/order/build.html',
                        data: {
                            userid: $cookieStore.get('login').id,
                            houseid: $stateParams.id,
                            type: $scope.level,
                            startTime: Date.parse(date) / 1000,
                            endTime: Date.parse(date) / 1000,
                            dayNum: 1
                        },
                        dataType: 'json',
                        headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
                        transformRequest: $scope.changeData
                    }).then(function successCallback(response) {
                        if (response.data.status == 101) {
                            $state.go('order', {title: 'order',  id:response.data.data })
                        } else if (response.data.status == 102) {
                            $.alert({
                                title: '错误提示',
                                text: response.data.data,
                            });
                        }else if(response.data.status==103){
                            $.alert({
                                title: '错误提示',
                                text: response.data.data,
                            });
                        }
                    }, function errorCallback(response) {
                        console.log(response)
                    });
                }
            } else {
                $.alert({
                    title: '错误提示',
                    text: '请选择租房方式',
                });
            }
        }


    }


}]);
